const path = require('path');

const PDFParser = require('pdf2json');
const ExcelJS = require('exceljs');
async function convertPdfToExcel(pdfPath, excelPath) {
    // 创建一个PDF解析器
    const pdfParser = new PDFParser();
    pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError));
    pdfParser.on("pdfParser_dataReady", async pdfData => {
        // 创建一个Excel工作簿
        const workbook = new ExcelJS.Workbook();
        const worksheet = workbook.addWorksheet('Sheet 1');
        const { Pages } = pdfData;
        console.log(JSON.stringify(Pages))
        let i = 0;
        while (i < Pages.length) {
            const page = Pages[i];
            // 获取页面的文本内容
            const textContent = page.Texts.map(text => decodeURIComponent(text.R[0].T));
            // 将文本内容写入Excel单元格
            worksheet.getRow(i + 1).values = textContent;
            i++;
        }
        // 保存Excel文件
        await workbook.xlsx.writeFile(excelPath);
        console.log('转换成功！Excel文件已保存。');
    });
    // 解析PDF文件
    pdfParser.loadPDF(pdfPath);
}

const fieldPath = '../testFiles/';
const pdfPath = path.join(__dirname, fieldPath +'a.pdf');
const excelPath = path.join(__dirname, fieldPath + 'a.xlsx');

convertPdfToExcel(pdfPath, excelPath);